#include <sstream>

#include "Cryptography.hpp"

std::string Cryptography::DecryptLoginPacket(std::string packet, std::size_t length)
{
	for (std::size_t i = 0; i < length; i++)
		packet[i] = (packet[i] - 0x0F) ^ 0x0C3;
	return packet;
}

std::string Cryptography::DecryptLoginPassword(std::string password)
{
	std::size_t remainder = password.length() % 4;
	password.erase(0, remainder != 0 ? remainder : 4);
	for (std::size_t i = 0; i < password.length(); i++)
		password.erase(i, 1);
	for (std::size_t i = 2; i < password.length(); i += 3)
		password.insert(i, " ");
	std::stringstream ss(password);
	password.clear();
	std::size_t temporary;
	while (ss >> std::hex >> temporary)
		password += temporary;
	return password;
}

std::string Cryptography::EncryptLoginPacket(std::string packet)
{
	for (std::size_t i = 0; i < packet.length(); i++)
		packet[i] += 0x0F;
	return packet;
}